import { createSlice, PayloadAction } from '@reduxjs/toolkit'

export interface IAuthInfoState {
  token: string
  refresh_token: string
  expiresAt: number // 13位时间戳
  userID: string
}

const initialState: IAuthInfoState = {
  token: "",
  refresh_token: "",
  expiresAt: 86400,
  userID: "",
}

export const authSlice = createSlice({
  name: 'AUTH',
  initialState,
  reducers: {
    saveAuthInfo: (state, action: PayloadAction<IAuthInfoState>) => {
      const {
        token,
        refresh_token,
        expiresAt,
        userID,
      } = action.payload
      state.token = token
      state.refresh_token = refresh_token
      state.expiresAt = expiresAt
      state.userID = userID
    }
  },
})

// Action creators are generated for each case reducer function
export const { saveAuthInfo } = authSlice.actions

export default authSlice.reducer